package org.example.myleet.p611;

import java.util.Arrays;

public class Solution {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int sum = 0;
        for (int i = 0; i < nums.length; ++i) {
            int k = i + 2;
            for (int j = i + 1; j < nums.length; ++j) {
                while (k < nums.length && nums[i] + nums[j] > nums[k]) {
                    ++k;
                }
                sum += Math.max(k - j - 1, 0);
            }
        }
        return sum;
    }
}
